package com.dap.utils;

import com.alibaba.simpleimage.SimpleImageException;
import com.alibaba.simpleimage.render.ReadRender;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;

/**
 * Created by wangmin on 2018/11/12.
 */
public class WechatQrCodeUtils {
    // 打印二维码参数
    private static class QrCodeImageParam {
        public final BufferedImage bufferedImage;

        public final int imageSize; // 必须是41的整数倍, 否则不能等比例缩放二维码

        public final int startX;
        public final int startY;

        public QrCodeImageParam(BufferedImage bufferedImage, int imageSize, int startX, int startY) {
            this.bufferedImage = bufferedImage;

            this.imageSize = imageSize;

            this.startX = startX;
            this.startY = startY;
        }
    }


    // 读取 微信传回的二维码png二进制数据
    public static BufferedImage readQrCodeBytes(byte[] bytes) throws SimpleImageException {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
        ReadRender readRender = new ReadRender(inputStream);
        BufferedImage qrCodeBufferedImage = readRender.render().getAsBufferedImage();
        return qrCodeBufferedImage.getSubimage(18, 18, 246, 246);    // 二维码区域为: x,y=[18, 264)
    }


    // 打印二维码
    private static void printQrCode(BufferedImage printedBufferedImage, QrCodeImageParam qrCodeImageParam) {
        BufferedImage bufferedImage = qrCodeImageParam.bufferedImage;

        final double t = ((double) qrCodeImageParam.bufferedImage.getWidth()) / qrCodeImageParam.imageSize;
        for (int x = 0; x< qrCodeImageParam.imageSize; ++x) {
            final int fromX = (int)(x*t);
            for (int y = 0; y< qrCodeImageParam.imageSize; ++y) {
                final int fromY = (int)(y*t);
                int rgb = bufferedImage.getRGB(fromX, fromY);
                printedBufferedImage.setRGB(x+ qrCodeImageParam.startX, y+ qrCodeImageParam.startY, rgb);
            }
        }
    }

    /**
     * @param printedBufferedImage 被打印的图片
     * @param qrCodeBytes 微信传回的二维码png二进制数据
     * @param imageSize 必须是41的整数倍, 否则不能等比例缩放二维码
     * @param startX 打印到printedBufferedImage的起始X坐标
     * @param startY 打印到printedBufferedImage的起始Y坐标
     * */
    public static void printQrCode(BufferedImage printedBufferedImage, byte[] qrCodeBytes, int imageSize, int startX, int startY) throws SimpleImageException {
        BufferedImage qrCodeBufferedImage = readQrCodeBytes(qrCodeBytes);
        QrCodeImageParam qrCodeImageParam = new QrCodeImageParam(qrCodeBufferedImage, imageSize, startX, startY);
        printQrCode(printedBufferedImage, qrCodeImageParam);
    }
    /**
     * @param printedBufferedImage 被打印的图片
     * @param qrCodeBufferedImage 二维码的图片
     * @param imageSize 必须是41的整数倍, 否则不能等比例缩放二维码
     * @param startX 打印到printedBufferedImage的起始X坐标
     * @param startY 打印到printedBufferedImage的起始Y坐标
     * */
    public static void printQrCode(BufferedImage printedBufferedImage, BufferedImage qrCodeBufferedImage, int imageSize, int startX, int startY) throws SimpleImageException {
        QrCodeImageParam qrCodeImageParam = new QrCodeImageParam(qrCodeBufferedImage, imageSize, startX, startY);
        printQrCode(printedBufferedImage, qrCodeImageParam);
    }
}
